<template>
  <el-dialog
    width="20%"
    title="新增目录"
    :visible.sync="dialogFormVisible"
    :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="ruleForm">
      <el-form-item
        v-if="!isSubjects"
        label="所属学科"
        :label-width="formLabelWidth"
        prop="subjectID"
      >
        <el-select v-model="form.subjectID" placeholder="请选择活动区域">
          <!--  <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option> -->
          <el-option
            v-for="item in list"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        prop="tagName"
        label="目录名称"
        :label-width="formLabelWidth"
      >
        <el-input v-model.trim="form.tagName" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'MyDialog',
  props: {
    // 是否打开弹框判断属性
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    // 学科列表
    list: Array,
    // 学科id
    id: [Number, Object, String],
    // 是否学科跳转过来
    isSubjects: {
      type: Boolean,
      // 指定当前属性为必填项 不填或没有默认值提示 Missing required prop: 缺少必填属性
      required: true
    },
    // 是否修改
    isChangeDate: Boolean,
    // 修改数据
    cData: Object,
    cID: Number
  },
  emits: ['myConfirm'],
  data () {
    return {
      flag: 0,
      dialogTableVisible: false,
      form: {
        subjectID: '',
        tagName: ''
      },
      formLabelWidth: '70px',
      rules: {
        subjectID: [
          { required: true, message: '请选择学科', trigger: 'change' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
        ]

      }
    }
  },
  watch: {
    dialogFormVisible (newVal, oldVal) {
      if (newVal && this.flag !== 0) {
        this.$refs.ruleForm.resetFields()
      }
      // 是否修改数据
      if (this.isChangeDate) {
        this.form = this.cData
      }
      if (!newVal) {
        this.$emit('update:isChangeDate', false)
      }
    }
  },

  methods: {
    // 取消
    cancel () {
      this.close()
    },
    // 确定
    confirm () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 是否修改数据
          if (this.isChangeDate) {
            const { subjectID, tagName } = this.form
            const data = {
              subjectID,
              tagName,
              id: this.cID
            }
            this.$emit('myConfirm', data, false)
          } else {
            // 获取数据 //isSubjects
            if (this.isSubjects) this.form.subjectID = this.id
            console.log(this.id)
            // 触发自定义事件传值给父组件
            this.$emit('myConfirm', this.form, true)
          }

          this.close()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // x
    handleClose (done) {
      this.close()
    },

    close () {
      if (this.flag === 0) this.flag = 1
      this.$emit('update:dialogFormVisible', false)
      this.$emit('update:isChangeDate', false)
      // this.$refs.ruleForm.resetFields()
    }

  }
}
</script>

<style scoped lang='less'>
.el-select {
  width: 100%;
}
/deep/ .el-form-item__label {
  // font-size: 12px;
  width: 78px !important;
}
/deep/ .el-form-item__content {
  margin-left: 78px !important;
}
</style>
